package cn.zc.gateway.service;


import cn.tools.common.enums.AuthEnum;
import cn.tools.jwt.server.service.JwtTokenDecodeService;

import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Jwt;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: e
 * @date: 2021/12/16
 * @description: 判断token
 */

@Service
public class AnalysisTokenServiceImpl {

    @Resource
    private JwtTokenDecodeService jwtTokenDecodeService;


    /**
     * @author: keno
     * @date:   2021/12/24
     * @description: jwtTokenDecodeService无法注入一直为null，
     * 后续采用@PostConstruct设定静态初始化，保证注入
     * 说明：
     * 作用：
     * @PostConstruct注解的方法在项目启动的时候执行这个方法，也可以理解为在spring容器启动的时候执行，可作为一些数据的常规化加载，比如数据字典之类的
     */
    private static AnalysisTokenServiceImpl analysisTokenService;
    @PostConstruct
    public void init(){
        analysisTokenService = this;
        analysisTokenService.jwtTokenDecodeService = this.jwtTokenDecodeService;
    }


    public ServerHttpRequest decodeHS256(ServerHttpRequest request,String token){
        //解密获取出token中携带的参数
        Jwt jwt = jwtTokenDecodeService.tokenDecode(token);

        Map<String,String> body = (Map<String, String>) jwt.getBody();
        ServerHttpRequest requestHead = request.mutate().header(AuthEnum.AUTHORIZATION.getValue(),body.get("userInfo") ).build();

        return requestHead;
    }
}
